/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.dispatcher;

import com.jspx.boot.EnvFactory;
import com.jspx.boot.JspxNetApplication;
import com.jspx.boot.environment.Environment;
import com.jspx.boot.environment.EnvironmentTemplate;
import com.jspx.utils.FileUtil;
import com.jspx.utils.StringUtil;
import com.jspx.utils.URLUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-12-26
 * Time: 17:42:15
 * tomcat get 方法需要设置编码修复
 *  这个东西会占用 request 读一次，不能正常使用ajax 请求
 *
 */
public class FilterDispatcher implements Filter {
    private String filterSuffix = null;
    private String templateSuffix = null;

    public FilterDispatcher() {

    }

    /**
     * @param servletConfig 配置
     * @throws ServletException 异常
     */
    public void init(FilterConfig servletConfig) throws ServletException {
        String defaultPath = servletConfig.getServletContext().getInitParameter("defaultPath");

        if (!StringUtil.isNULL(defaultPath))
        {
            JspxNetApplication.autoRun(defaultPath);
        } else {
            String filePath = servletConfig.getServletContext().getRealPath("/");
            if (!StringUtil.isNULL(filePath)) filePath = FileUtil.mendPath(filePath + "WEB-INF/classes");
            if (!StringUtil.isNULL(filePath)&&FileUtil.isDirectory(filePath))
            {
                JspxNetApplication.autoRun(FileUtil.mendPath(filePath));
            }
            else   JspxNetApplication.autoRun();
        }

        String realPath = servletConfig.getServletContext().getInitParameter("realPath");
        if (StringUtil.isNULL(realPath))
            realPath = servletConfig.getServletContext().getRealPath("/");
        Dispatcher.setRealPath(FileUtil.mendPath(realPath));

    }

    /**
     * @param servletRequest  请求
     * @param servletResponse 应答
     * @param filterChain     过滤
     * @throws ServletException 异常
     * @throws IOException      异常
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String urlName = URLUtil.getFileName(request.getRequestURI());
        //确保系统跳转到正确到后缀名begin
        if (!StringUtil.toBoolean(request.getParameter("t")) && (StringUtil.isNULL(urlName) || urlName.equals("") || urlName.equals("/"))) {
            response.sendRedirect(request.getRequestURI() + "index." + filterSuffix + "?t=true");
            return;
        }
        //确保系统跳转到正确到后缀名end

        String uri = request.getRequestURI();
        if (uri == null) uri = StringUtil.empty;
        if (!uri.toLowerCase().endsWith(templateSuffix) && !uri.toLowerCase().endsWith(filterSuffix))
        {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            Dispatcher.getInstance().wrapRequest(request, response);
        }
    }

    /**
     * 卸载数据
     */
    public void destroy() {
        JspxNetApplication.destroy();
    }
}